package com.workpaper.app.tray;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ResourceLocator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;

import com.workpaper.app.cache.resource.ViewInfoCache;
import com.workpaper.app.consts.AppConsts;
import com.workpaper.app.consts.path.ImagePath;
import com.workpaper.app.consts.tray.TrayViewConsts;

/**
 * 系统托盘项
 * @author zhouyujun
 *
 */
public class AppTrayItem{
	/**
	 * 系统托盘项
	 */
	private TrayItem trayItem = null;
	/**
	 * 系统托盘
	 */
	private Tray tray;
	/**
	 * 右键监听
	 */
	private TrayItemRightClickListener rightClickListener;
	
	/**
	 * 单例模式
	 * @author zyj
	 *
	 */
	private static class Holder{
		private static AppTrayItem INSTANCE = new AppTrayItem();
	}
	public static AppTrayItem getInstance() {
		return Holder.INSTANCE;
	}
	private AppTrayItem() {
		super();
	}
	
	public void createSystemTrayItem() {
		//获取系统托盘对象
		tray = Display.getCurrent().getSystemTray();
		//创建托盘项
		trayItem = new TrayItem(tray, SWT.NONE);
		trayItem.setText(ViewInfoCache.get(TrayViewConsts.trayItemText));
		//图标
		ImageDescriptor trayIcon = ResourceLocator.imageDescriptorFromBundle(AppConsts.APP_ID, ImagePath.tray_item_icon_path).get();
		trayItem.setImage(trayIcon.createImage());

		//设置监听处理
		trayItem.addSelectionListener(new TrayItemClickListener());
		rightClickListener = new TrayItemRightClickListener();
		trayItem.addListener(SWT.MenuDetect, rightClickListener);
	}
	
	/**
	 * 销毁资源
	 */
	public void dispose() {
		rightClickListener.dispose();
		trayItem.dispose();
		tray.dispose();
	}
}
